<?php 

	class Feedback extends MY_Controller {

		public function __construct() {
			parent::__construct();
			$this->load->model('Feedback_model', 'feedback');
			$this->load->model('Category_model', 'c');
			// 产品分类
			$data = $this->c->getCateData('production',array());
			$this->_d['pcate'] = cate2list(0, $data);
		}

		public function index()
		{
			if ($this->form_validation->run('feedback/add') == false)
			{
				if (validation_errors() != '')
				{
					$retmsg['code'] = '0';
					$retmsg['msg'] = html2text(validation_errors());
					exit(json_encode($retmsg));
				}
				
				$sdata['show'] = 1;

				$feedback = $this->feedback->L($sdata,'*',$this->_p['pagenumb'], ($this->_p['pagecur'] == 0) ? 0 : ($this->_p['pagecur']-1) * $this->_p['pagenumb'],'ctime','desc');
				$this->_p['pagecount'] = $this->input->post('pagecount');
				if (empty($this->_p['pagecount'])) 
				{
					$this->_p['pagecount'] = $this->feedback->C($sdata);
				}
				$this->_d['page'] = eyPage($this->_p,$sdata);
				$this->_d['pagecount'] = $this->_p['pagecount'];
				$this->_d['feedback'] = $feedback;
				$this->load->view($this->_d['cfg']['tpl'] . 'feedback', $this->_d);
			}
			else
			{
				$postdata = $this->input->post();
				$vcode = $postdata['vcode'];
				if(strtolower($vcode)!=$this->session->userdata("vcode"))
				{
					$retmsg['code'] = '0';
					$retmsg['msg'] = '请输入正确的验证码';
					exit(json_encode($retmsg));
				}
				$postdata['ctime'] = time();
				$postdata['ip_address'] = $this->input->ip_address();
				if ($this->feedback->A($postdata))
				{
					$retmsg['code'] = '1';
					$retmsg['msg'] = "感谢您的留言,我们会及时给您回复";
					exit(json_encode($retmsg));
				}
				else
				{
					$retmsg['code'] = '0';
					$retmsg['msg'] = "递交失败，请重试";
					exit(json_encode($retmsg));
				}

			}
			
		}
	}